/**
 * @file Condition.cc
 * @author zziywang (zziywang@163.com)
 * @brief { life is too short to learn cpp! }
 * @version 1.0
 * @date 2022-05-08
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "Condition.hh"
#include "MutexLock.hh"
#include <stdio.h>

namespace snow
{

Condition::Condition(MutexLock &mutex)
    : mutex_(mutex)
{
    int ret = pthread_cond_init(&cond_, nullptr);
    if (0 != ret) {
        perror("pthread_cond_init");
    }
}
Condition::~Condition()
{
    int ret = pthread_cond_destroy(&cond_);
    if (0 != ret) {
        perror("pthread_cond_destroy");
    }
}

void Condition::wait()
{
    int ret = pthread_cond_wait(&cond_, mutex_.getMutexPtr());
    if (0 != ret) {
        perror("pthread_cond_wait");
        return;
    }
}
void Condition::notify()
{
    int ret = pthread_cond_signal(&cond_);
    if (0 != ret) {
        perror("pthread_cond_signal");
        return;
    }
}
void Condition::notifyAll()
{
    int ret = pthread_cond_broadcast(&cond_);
    if (0 != ret) {
        perror("pthread_cond_broadcast");
        return;
    }
}

} // namespace snow